<%--
  Created by IntelliJ IDEA.
  User: 74018
  Date: 2019-01-09
  Time: 2:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>订单列表</title>
    <jsp:include page="${pageContext.request.contextPath}/common/common.jsp"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3-editable/css/bootstrap-editable.css">
    <script src ="${pageContext.request.contextPath}/static/bootstrap3-editable/js/bootstrap-editable.js" ></script>
</head>
<body>
<div class="container">
    <div class="container">
        <table id="tb"></table>
    </div>
    <div class="container">
        <h3 class="text-center">订单使用零件</h3>
        <table id="tbs"></table>
    </div>
</div>
<form action="#" id="myTool" class="form-inline">
    <div class="form-group">
        <input type="text" class="form-control" name="orId" placeholder="订单编号">
    </div>
    <div class="form-group">
        <select name="gId" id="gid"  class="form-control"></select>
    </div>
    <div class="form-group">
        <input type="button" class="btn btn-primary" onclick="get();" value="搜索">
    </div>
</form>

<!-- 提交订单 -->
<div class="modal fade" id="sbmOrder" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modelTitleId">订单零件</h4>
            </div>
            <div class="modal-body">
                <div id="one" style="position:relative">
                    <div class="input-group" id="tool">
                                <input type="text" class="form-control">
                                <span class="input-group-btn"> <button type="button" class="btn btn-primary">搜索</button></span>
                            </div>
                    <div id="cDTb" style="position: absolute;top: 34px;background-color:#fff;z-index: 9999;width: 100%;">
                        <table id="cTable" style="width: 100%;"></table>
                    </div>
                </div>
                <table id="coTB"></table>
                <input type="hidden" name="oid" id="oid" >
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="doAdd();">确定</button>
            </div>
        </div>
    </div>
</div>

<!--审核-->
<div class="modal fade" id="shenHe" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modelTitleId1">审核</h4>
            </div>
            <div class="modal-body">
                <form action="#" class="form-horizontal">
                    <input type="hidden" id="okId" name="oid" />
                    <div class="form-group">
                        <button type="button" class="btn btn-secondary form-control" onclick="reOrder()">重新维修</button>
                    </div>
                    <div class="form-group">
                        <button type="button" class="btn btn-secondary form-control" onclick="okOrder()">完成订单</button>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="modelId" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog modal-sm|lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="222">重新维修</h4>
            </div>
            <div class="modal-body">
                <form action="#" class="form-horizontal">
                    <div class="form-group">
                        <input type="text" class="form-control" name="note" id="reNote" placeholder="填写原因">
                    </div>
                    <input type="hidden" name="oid">
                    <%--<input type="button" value="提交" class="btn-primary btn" />--%>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary"  onclick="doReOrder();">确认</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script>
    $(function () {
        $("#tb").bootstrapTable({
            url:"${pageContext.request.contextPath}/repairOrder/getOrder",
            toolbar:'#myTool',//工具按钮用哪个容器
            striped: true,//隔行换色
            //showRefresh: true,//是否显示刷新按钮
            clickToSelect: true,
            height: 300,//行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "id",//每一行的唯一标识，一般为主键列
            //showToggle:true,//是否显示详细视图和列表视图的切换按钮
            pagination:true, //是否显示分页（*）
            detailView:true,//是否显示父子表
            onExpandRow:function(index,row,$detail){
                var table = $detail.html("<table class='myTb'></table>").find("table");
                table.bootstrapTable({
                    url:'${pageContext.request.contextPath}/repairOrder/getOrderInfo?id='+row.id+'',
                    uniqueId: "id",
                    columns:[{
                        checkbox:false
                    },{
                        field:'id',
                        title:'编号'
                    },{
                        field:'name',
                        title:'名称'
                    },{
                        field:'price',
                        title:'价钱'
                    },{
                        field:'note',
                        title:'备注'
                    }]
                })},
            pageSize : 10,//单页记录数
            pageList : [ 10, 20, 30 ],//可选择单页记录数
            columns:[
                {field:'id',title:'编号' },
                {field:'orId',title:'订单编号' },
                {field:'gName',title:'客户'},
                {field:'carNo',title:'车牌'},
                {field:'date',title:'订单时间'},
                {field:'staffName', title:'处理员工'},
                {field:'userName', title:'发布人'},
                {field:'stateName',title:'状态'},
                {field:'moeny', title:'金额'},
                {field:'note',title:'备注'},
                {title:'操作',
                    formatter:function(value,row,index){
                        if (row.state==1){
                            return "<button class='btn btn-primary' onclick='startOrder("+row.id+")'><span class='glyphicon glyphicon-pencil'></span>&nbsp;开始订单</button>";
                        } else if (row.state==2||row.state==4){
                            return "<button class='btn btn-primary' onclick='sbmOrder("+row.id+")'><span class='glyphicon glyphicon-pencil'></span>&nbsp;提交</button>";
                        } else if (row.state==7){
                            return "<button class='btn btn-primary' onclick=''><span class='glyphicon glyphicon-pencil'></span>&nbsp;交易完成</button>";
                        } else{
                            var role = ${role.id};
                            if (role==4){
                                return "<button class='btn btn-primary' onclick='shenHe("+row.id+")'><span class='glyphicon glyphicon-pencil'></span>&nbsp;审核</button>";
                            }else if(row.state==3){
                                return "<button class='btn btn-primary'><span class='glyphicon glyphicon-pencil'></span>&nbsp;审核中</button>";
                            }else{
                                return "<button class='btn btn-primary'><span class='glyphicon glyphicon-pencil'></span>&nbsp;交易完成</button>";
                            }
                        }
                    }
                }
                ],
            onClickRow:function (row) {
                $.post("${pageContext.request.contextPath}/repairOrder/getByRoid",{"roid":row.id},function (data) {
                    $("#tbs").bootstrapTable("load",data);
                });
            }
        });
        $("#cDTb").hide();
        $("#tool").bind({"click":function(){
                $("#cDTb").show();
                $(document).click(function(e){
                    var target = $(e.target);
                    if(!target.is('#cDTb')&&!target.is('#tool input')){
                        $("#cDTb").hide();
                    }
                })
            }
        });
        //用到的零件
        $("#tbs").bootstrapTable({
            url:"${pageContext.request.contextPath}/repairOrder/getByRoid?roid=0",
            height: 200,
            columns:[
                {field:'id',title:'编号' },
                {field:'name',title:'名称' },
                {field:'count',title:'数量' },
                {field:'price',title:'价格' }
            ]
        });
        $("#cTable").bootstrapTable({
            url:"${pageContext.request.contextPath}/compone/comquery",
            height: 160,
            columns:[

                {field:'id',title:'编号' },
                {field:'purchase.name',title:'名称' },
                {field:'count',title:'库存' },
                {field:'price',title:'价格' }
            ],
            onClickRow:function (row) {
                if (row.count>0){
                    var chek = $("#coTB").bootstrapTable("getRowByUniqueId",row.id);
                    if (chek!=null&&chek!=''){
                        chek.count = parseInt(chek.count)+1;
                        $.post("${pageContext.request.contextPath}/compone/getCount",{"id":row.id},function (data) {
                            if(chek.count>data){
                                chek.count = data;
                                swal("提示信息", "库存不足!("+data+")", "error");
                            }
                            $("#coTB tr[data-uniqueid="+chek.id+"] td:eq(3)").val(chek.count);
                            $("#coTB tr[data-uniqueid="+chek.id+"] td:eq(5)").val(chek.count*chek.price);
                            var index = $("#coTB tr[data-uniqueid="+chek.id+"]").attr("data-index");
                            $('#coTB').bootstrapTable('updateRow', {index: index, row: chek})
                        });
                    }else{
                        row.column = true;
                        row.count =1;
                        $('#coTB').bootstrapTable('insertRow', {
                            index:$('#coTB').bootstrapTable('getOptions').totalRows,
                            row:row
                        });
                    }
                }else{
                    swal("提示信息", "需要进货!", "error");
                }
            }
        });
        $("#coTB").bootstrapTable({
            height: 160,
            uniqueId:"id",
            columns:[
                {field:'column',checkbox:true},
                {field:'id',title:'编号' },
                {field:'purchase.name',title:'名称' },
                {field:'count',width:"20%",title:'数量',formatter:function(value,row,index){
                        return "<input type='text' name='count' value='"+row.count+"'  class='form-control' onblur='upCount("+index+","+row.id+",this)'/>";
                    } },
                {field:'price',title:'价格' },
                {field:'xj',title:'小计',formatter:function(value,row,index){
                    return row.count*row.price;
                    }},
                {title:'操作',formatter:function(value,row,index){
                    return "<button class='btn btn-primary' onclick='delRow("+row.id+")'><span class='glyphicon glyphicon-pencil'></span>&nbsp;删除</button>";
                    }
                }
            ]
        })
    });

    function startOrder(id) {
        $.post("${pageContext.request.contextPath}/repairOrder/upOrderState",{"oid":id, "state":2},function (data) {
            if (data>0){
                swal("提示信息", "成功!", "success");
            } else{
                swal("提示信息", "修改失败!", "error");
            }
            $.post("${pageContext.request.contextPath}/repairOrder/getOrder",function (data) {
                $("#tb").bootstrapTable("load",data);
            });
        })
    }

    function sbmOrder(id){
        $("#sbmOrder").modal("show");
        $("#coTB").bootstrapTable("removeAll");
        $("#oid").val(id);
     }
    function doAdd() {
        var rows = $("#coTB").bootstrapTable("getAllSelections");
        console.log(rows);
        var oid = $("#oid").val();
        var cidStr = '';
        var count = '';
        $.each(rows, function (index,item) {
            cidStr += item.id+"-";
            count += item.count+'-';
        })
        $.post("${pageContext.request.contextPath}/repairOrder/subOrder",{"roId":oid,"cid":cidStr,"countStr":count},function (data) {
            if (data>0){
                swal("提示信息", "成功!", "success");
            } else{
                swal("提示信息", "修改失败!", "error");
            }
            $.post("${pageContext.request.contextPath}/repairOrder/getOrder",function (data) {
                $("#tb").bootstrapTable("load",data);
            });
        });
        $("#sbmOrder").modal("hide");
    }
    function delRow(id){
        console.log(id);
        $("#coTB").bootstrapTable("removeByUniqueId",id);
    }
    function upCount (index,id,ele){
        var count = $(ele).val();
        $.post("${pageContext.request.contextPath}/compone/getCount",{"id":id},function (data) {
            if (count>data){
                count = data;
                swal("提示信息", "库存不足!("+data+")", "error");
                $(ele).val(""+data+"");
            }

            var row = $("#coTB").bootstrapTable("getRowByUniqueId",id);
            console.log(row.id);

            row.count = count;
            $("#coTB tr[data-uniqueid="+id+"] td:eq(5)").val(row.count*row.price);
            $('#coTB').bootstrapTable('updateRow', {index: index, row: row})

        });

    }
    function reOrder() {
        $("#modelId").modal("show");
    }
    function doReOrder() {
        var oid = $("#okId").val();
        var note = $("#reNote").val();
        $.post("${pageContext.request.contextPath}/repairOrder/backOrder",{"oid":oid,"state":4,"note":note},function (data) {
            if (data>0){
                swal("提示信息", "成功!", "success");
            } else{
                swal("提示信息", "失败!", "error");
            }
            $("#modelId").modal("hide");
        });
        $.post("${pageContext.request.contextPath}/repairOrder/getOrder",function (data) {
            $("#tb").bootstrapTable("load",data);
        });
    }
    function okOrder() {
        var oid = $("#okId").val();
        $.post("${pageContext.request.contextPath}/repairOrder/upOrderState",{"oid":oid,"state":7},function (data) {
            if (data>0){
                swal("提示信息", "成功!", "success");
            } else{
                swal("提示信息", "失败!", "error");
            }
        });
    }
    function shenHe(id) {
        $("#okId").val(id);
        $("#shenHe").modal("show");
    }
    $(function () {
        $.post('${pageContext.request.contextPath}/guests/query',function(data){
            var s = "" ;
            s+= "<option value='0'>请选择</option>" ;
            $.each(data,function (index,item) {
                s = s + "<option value="+item.id+">"+item.name+"</option>";
                console.log(item.name);
            })
            $("#gid").html(s) ;
        }) ;
    })
    function get() {
        var orId = $("#myTool [name=orId]").val();
        var gId = $("#myTool [name=gId]").val();
        $.post("${pageContext.request.contextPath}/repairOrder/getOrder",{"gId":gId,"orId":orId},function (data) {
            console.log(data);
           $("#tb").bootstrapTable("load",data);

        });
    }
</script>
